<?php
require_once('../../../config.php');
require_once($CFG->libdir . '/adminlib.php');
require_once($CFG->dirroot . '/local/itusurveyplugin/lib.php');
require_once($CFG->dirroot . "/local/itusurveyplugin/forms.php");

//Require authentication and authorization
require_login($SITE);
require_capability('local/itusurveyplugin:managequestion', context_system::instance());

//prepare moodle_url
global $DB; 
$path = $CFG->wwwroot.'/local/itusurveyplugin/ituquestion/ituquestionmanagement.php';
$path2 = $CFG->wwwroot.'/local/itusurveyplugin/ituquestion/itu_modify_question.php';
//Load custom fields
//$load = optional_param('load', null, PARAM_TEXT);

/* Chuan bi trang web can hien thi */	
$url = new moodle_url($path);
$PAGE->set_url($url);
$PAGE->set_title('ITU Question Management');
$PAGE->set_heading('ITu Question Management');
$PAGE->set_pagelayout('standard');

navigation_node::override_active_url(new moodle_url($path, array('param'=>'value')));

echo $OUTPUT->header();

//Include functions
$mylib = new question_management();
//New form instance
$mform = new form_question_management(null, null);
$mform->display();


//cancel action
if ($mform->is_cancelled()) {

   echo '<script>location.href="'.$path.'?&load='.$reload.'"</script>';  
        echo '<script>clear_form_elements("#mform1");</script>' ;

}

//submit action 
if ($fromform = $mform->get_data()) 
{	
    $fromform = $mform->get_data();
    
    $keyword = $fromform->txtKeyword;
    $type_QT = $fromform->qt_type;
    //echo "<script>alert('".$type."')</script>";
    $status = $fromform->qt_status;
    
    echo '<table class="generalbox" border="1" align="center">';
	echo '<tbody>';
    echo '<tr>'; 
    echo '<th class="headingblock header" >'.get_string('questionContent','local_itusurveyplugin').'</th>'; 
    echo '<th class="headingblock header">'.get_string('TypeQuestion','local_itusurveyplugin').'</th>';
   
    echo '<th class="headingblock header">'.get_string('updatedDate','local_itusurveyplugin').'</th>';
    echo '<th class="headingblock header">'.get_string('status','local_itusurveyplugin').'</th>';
    echo '<th class="headingblock header">'.get_string('Edit','local_itusurveyplugin').'</th>';
    echo '<th class="headingblock header">'.get_string('Clone','local_itusurveyplugin').'</th>';
    echo '<th class="headingblock header">'.get_string('delete','local_itusurveyplugin').'</th>';
    echo '</tr>';
     
    $sql = "select * from mdl_itu_questions qt where qt.q_content like '%".$keyword."%'";
    
    if($type_QT != 0){
        $sql = $sql."AND qt.q_question_type ='".$type_QT."'";
    }
    if($status == 1)
    $sql = $sql."AND qt.id IN(select distinct qi_question from mdl_question_itus)";
    if ($status == 2)
    $sql = $sql."AND qt.id NOT IN(select distinct qi_question from mdl_question_itus) ";
    $questions = $DB->get_records_sql($sql);
    foreach ($questions as $question)
    {
    echo '<tr>';
    echo '<td class="question" style="cursor:pointer;">'.$question->q_content.'</td>'; 
    $type = $DB->get_record('itu_question_types',array('id'=>$question->q_question_type));
    echo '<td class="question" style="cursor:pointer;">'.$type->qt_content.'</td>'; 
    //echo "<script>alert('".$SESSION->lang."')</script>";
    
    $curLang = $SESSION->lang;

    if($curLang == 'en'|| $curLang==NULL)
    {
    $year = substr($question->q_update_date,0,4);
    $month = substr($question->q_update_date,5,2);
    $day = substr($question->q_update_date,8,2);
    $hour = substr($question->q_update_date,11,2);
    $minute = substr($question->q_update_date,14,2);
    $second = substr($question->q_update_date,17,2);
    //echo"<script>alert('".$second."')</script>";
    $date_en = $month.'-'.$day.'-'.$year.' '.$hour.':'.$minute.':'.$second;
    echo '<td class="question" style="cursor:pointer;">'.$date_en.'</td>';
    }
    
    if($curLang == 'vi')
    {
    $year = substr($question->q_update_date,0,4);
    $month = substr($question->q_update_date,5,2);
    $day = substr($question->q_update_date,8,2);
    $hour = substr($question->q_update_date,11,2);
    $minute = substr($question->q_update_date,14,2);
    $second = substr($question->q_update_date,17,2);
    //echo"<script>alert('".$second."')</script>";
    $date_vi = $day.'-'.$month.'-'.$year.' '.$hour.':'.$minute.':'.$second;
    echo '<td class="question" style="cursor:pointer;">'.$date_vi.'</td>';
    }
    
    $used_qt = $DB->get_records('question_itus');
    $i=0;
    foreach($used_qt as $used)
    {
        if($used->qi_question == $question->id)
        $i++;
        
    }
    if($i!=0)
    echo '<td class="question" style="cursor:pointer;">'.get_string('Used','local_itusurveyplugin').'</td>';
    else
    echo '<td class="question" style="cursor:pointer;">'.get_string('NoUsed','local_itusurveyplugin').'</td>';
    
    echo '<td class="question" style="cursor:pointer;">';
    if($i==0)
    {
    echo "<form action='itu_modify_question.php' method='POST'>";
    echo "<input type='hidden' name='type_submit' value='edit'/>";
    echo "<input type='hidden' name='id' value='".$question->id."'/>";
    echo "<button style='width: 25px' type='submit'><img src='../css/images/edit.jpg'></button>";
    echo "</form>";
    }
    echo '</td>';
    echo '<td align="center">';
    echo "<form action='itu_modify_question.php' method='POST'>";
    echo "<input type='hidden' name='type_submit' value='clone'/>";
    echo "<input type='hidden' name='id' value='".$question->id."'/>";
    echo "<button style='width: 25px' type='submit'><img src='../css/images/clone-icon.gif'></button>";
    echo "</form>";
    
    if($i ==0)
    {
    echo '</td>';
    echo "<form action='itu_delete_question.php' method='POST' onsubmit=\"return confirm('".get_string('deleteconfirm','local_itusurveyplugin')."');\">";
    echo "<input type='hidden' name='id' value='".$question->id."'/>";
    echo '<td class="question" style="cursor:pointer;">';
   
    echo "<button style='width: 25px' type='submit'><img src='../css/images/delete.png'></button>";
    echo '</td>';
    }
    echo "</form>";
    
    echo '</tr>';
    }
    echo '</tbody>';
    echo '</table>';
}
//Invalid Received Data or First Load
else{
    
    echo '<table class="generalbox" border="1" align="center">';
	echo '<tbody>';
    echo '<tr>'; 
    echo '<th class="headingblock header" >'.get_string('questionContent','local_itusurveyplugin').'</th>'; 
    echo '<th class="headingblock header">'.get_string('TypeQuestion','local_itusurveyplugin').'</th>';
    echo '<th class="headingblock header">'.get_string('updatedDate','local_itusurveyplugin').'</th>';
    echo '<th class="headingblock header">'.get_string('status','local_itusurveyplugin').'</th>';
    echo '<th class="headingblock header">'.get_string('Edit','local_itusurveyplugin').'</th>';
    echo '<th class="headingblock header">'.get_string('Clone','local_itusurveyplugin').'</th>';
    echo '<th class="headingblock header">'.get_string('delete','local_itusurveyplugin').'</th>';
    echo '</tr>';
     
    $sql = "select * from mdl_itu_questions"; 
       
    $questions = $DB->get_records_sql($sql);
    foreach ($questions as $question)
    {
    echo '<tr>';
    echo '<td class="question" style="cursor:pointer;">'.$question->q_content.'</td>'; 
    $type = $DB->get_record('itu_question_types',array('id'=>$question->q_question_type));
    echo '<td class="question" style="cursor:pointer;">'.$type->qt_content.'</td>'; 
    
    //$curLang = $CFG->lang;
    $curLang = $SESSION->lang;
    //echo"<script>alert('".$lang."')</script>";
    //echo"<script>alert('".$curLang."')</script>";
    
    if($curLang == 'en'|| $curLang==NULL)
    {
    $year = substr($question->q_update_date,0,4);
    $month = substr($question->q_update_date,5,2);
    $day = substr($question->q_update_date,8,2);
    $hour = substr($question->q_update_date,11,2);
    $minute = substr($question->q_update_date,14,2);
    $second = substr($question->q_update_date,17,2);
    //echo"<script>alert('".$second."')</script>";
    $date_en = $month.'-'.$day.'-'.$year.' '.$hour.':'.$minute.':'.$second;
    echo '<td class="question" style="cursor:pointer;">'.$date_en.'</td>';
    }
    
    if($curLang == 'vi')
    {
    $year = substr($question->q_update_date,0,4);
    $month = substr($question->q_update_date,5,2);
    $day = substr($question->q_update_date,8,2);
    $hour = substr($question->q_update_date,11,2);
    $minute = substr($question->q_update_date,14,2);
    $second = substr($question->q_update_date,17,2);
    //echo"<script>alert('".$second."')</script>";
    $date_vi = $day.'-'.$month.'-'.$year.' '.$hour.':'.$minute.':'.$second;
    echo '<td class="question" style="cursor:pointer;">'.$date_vi.'</td>';
    }
    
    $used_qt = $DB->get_records('question_itus');
    $i=0;
    foreach($used_qt as $used)
    {
        if($used->qi_question == $question->id)
        $i++;
        
    }
    if($i!=0)
    echo '<td class="question" style="cursor:pointer;">'.get_string('Used','local_itusurveyplugin').'</td>';
    else
    echo '<td class="question" style="cursor:pointer;">'.get_string('NoUsed','local_itusurveyplugin').'</td>';
    
    echo '<td class="question" align="center" style="cursor:pointer;">';
    if($i==0)
    {
    echo "<form action='itu_modify_question.php' method='POST'>";
    echo "<input type='hidden' name='type_submit' value='edit'/>";
    echo "<input type='hidden' name='id' value='".$question->id."'/>";
    
    echo "<button style='width: 25px' type='submit'><img src='../css/images/edit.jpg'></button>";
   
    echo "</form>";
    }
    echo '</td>';
    
    echo '<td align="center">';
    echo "<form action='itu_modify_question.php' method='POST'>";
    echo "<input type='hidden' name='type_submit' value='clone'/>";
    echo "<input type='hidden' name='id' value='".$question->id."'/>";
    echo "<button style='width: 25px' type='submit'><img src='../css/images/clone-icon.gif'></button>";
    echo "</form>";
    echo '</td>';
    
    if($i ==0)
    {
    echo "<form action='itu_delete_question.php' method='POST' onsubmit=\"return confirm('".get_string('deleteconfirm','local_itusurveyplugin')."');\">";
    echo "<input type='hidden' name='id' value='".$question->id."'/>";
    echo '<td class="question" style="cursor:pointer;">';
    
    echo "<button style='width: 25px' type='submit'><img src='../css/images/delete.png'></button>";
    echo '</td>';
    echo "</form>";
    }
    echo '</tr>';
    }
    echo '</tbody>';
    echo '</table>';
}

echo $OUTPUT->footer();
?>